﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xts
{
    namespace IceRuntime
    {
        public class XStormBase
        {
            protected Ice.Communicator x_ic;
            protected Ice.Logger x_logger;
            protected Dictionary<string, string> x_qos;
            protected IceStorm.TopicManagerPrx x_topicManagerPrx;
            private Ice.Communicator ic;
            private Ice.Logger logger;

            public XStormBase(ref Ice.Communicator ic, ref Ice.Logger logger)
            {
                x_ic = ic;
                x_logger = logger;
                x_qos = new Dictionary<string, string>();
                x_topicManagerPrx = null;
            }

            public XStormBase(Ice.Communicator ic, Ice.Logger logger)
            {
                // TODO: Complete member initialization
                this.ic = ic;
                this.logger = logger;
            }

            public bool IsTopicManagerValid
            {
                get
                {
                    return x_topicManagerPrx != null;
                }
            }

            public bool InitTopicManager(string topicManagerDsb)
            {
                if (IsTopicManagerValid)
                {
                    x_logger.error("Topic Manager is already initialized.");
                    return false;
                }

                if (x_ic.isShutdown())
                {
                    x_logger.error("Shutdown is already called on Ice.Communicator.");
                    return false;
                }

                try
                {
                    Ice.ObjectPrx topicMngPrx = x_ic.stringToProxy(topicManagerDsb);
                    x_topicManagerPrx = IceStorm.TopicManagerPrxHelper.checkedCast(topicMngPrx);
                    if (x_topicManagerPrx == null)
                    {
                        x_logger.error("Proxy: " + topicManagerDsb + " is invalid");
                        return false;
                    }
                    return true;
                }
                catch (Ice.Exception ex)
                {
                    x_logger.error(ex.Message);
                }
                return false;
            }

            protected IceStorm.TopicPrx RetrieveTopic(string topicName)
            {
                if (!IsTopicManagerValid)
                {
                    x_logger.error("Error: Topic Manager is not initialized.");
                    return null;
                }

                if (x_ic.isShutdown())
                {
                    x_logger.error("Error: Shutdown is already called on Ice.Communicator.");
                    return null;
                }

                IceStorm.TopicPrx topicPrx = null;
                try
                {
                    topicPrx = x_topicManagerPrx.retrieve(topicName);
                    return topicPrx;
                }
                catch (IceStorm.NoSuchTopic)
                {
                    try
                    {
                        topicPrx = x_topicManagerPrx.create(topicName);
                        return topicPrx;
                    }
                    catch (IceStorm.TopicExists)
                    {
                        x_logger.error(topicName + ": temporary error. try again.");
                    }
                }
                catch (Ice.Exception ex)
                {
                    x_logger.error(ex.Message);
                }

                x_logger.error(" Can't retrive topic: " + topicName);
                return null;
            }
        }
    }
}
